package com.slz.crm.pojo.vo;

import com.slz.crm.pojo.entity.PermissionsEntity;
import com.slz.crm.pojo.entity.RoleEntity;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

@Data
public class RoleANDPermissionVO {
    /**
     * 角色ID
     */
    private Long id;
    /**
     * 角色名称
     */
    private String roleName;
    /**
     * 角色描述
     */
    private String roleDesc;
    /**
     * 拥有权限列表
     */
    private List<PermissionVO> permissions;


    public static RoleANDPermissionVO formEntity(RoleEntity role, List<PermissionsEntity> permissions) {

        RoleANDPermissionVO vo = new RoleANDPermissionVO();
        BeanUtils.copyProperties(role, vo);
        vo.setPermissions(new ArrayList<>());

        permissions.forEach(entity -> {
            PermissionVO permissionVO = PermissionVO.fromEntity(entity);
            vo.getPermissions().add(permissionVO);
        });

        return vo;
    }

    public static RoleANDPermissionVO fromEntity(RoleVO role, List<PermissionsEntity> permissions) {
        RoleANDPermissionVO vo = new RoleANDPermissionVO();
        BeanUtils.copyProperties(role, vo);
        vo.setPermissions(new ArrayList<>());

        permissions.forEach(entity -> {
            PermissionVO permissionVO = PermissionVO.fromEntity(entity);
            vo.getPermissions().add(permissionVO);
        });

        return vo;
    }
}
